<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="分类名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分类名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:menu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click=""
          v-hasPermi="['system:menu:add']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="billCategoryList"
      row-key="id"
    >
      <el-table-column prop="id" label="ID" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="name" label="分类名字" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="pid" label="父级分类" :show-overflow-tooltip="true" width="160"></el-table-column>

      <el-table-column prop="status" label="类型" width="160">
        <template slot-scope="scope">
          <!-- 1是启用，2是停用 -->
          <span v-if="scope.row.status == 1">支出</span>
          <span v-if="scope.row.status == 2">收入</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="160">
        <template slot-scope="scope">
          <!-- 1是启用，2是停用 -->
          <span v-if="scope.row.status == 1">正常</span>
          <span v-if="scope.row.status == 2">删除</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="createUserAccount" label="创建人账户" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center" width="200"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:menu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:menu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="18" >
            <el-form-item label="分类名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入分类名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18" >
            <el-form-item label="类型" prop="type">
              <!-- <el-input v-model="form.name" placeholder="请输入分类名称" /> -->
              <el-radio-group v-model="form.type" change="getPids(value)">
                <el-radio :label="1">支出</el-radio>
                <el-radio :label="2">收入</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="父级分类">

              <el-select v-model="form.pid" clearable>
                <el-option
                  v-for="item in pids"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select> &nbsp;&nbsp;&nbsp;
              <el-tooltip content="增加一级分类，则不需要选择该项" placement="top">
                <i class="el-icon-question"></i>
              </el-tooltip>
            </el-form-item>
          </el-col >
          <el-col :span="18">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" maxlength="100" />
              </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>

</template>


<script>
import { listBillCategory, addBillCategory, delBillCategory, searchFirstCategory } from "@/api/bill";

export default {
  name: "billCategory",

  data() {
    return {
      // 遮罩层
      loading: true,
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 弹出层标题
      title: "",
      // 显示搜索条件
      showSearch: true,
      billCategoryList: [],
      queryParams:{
        name: undefined,
        status: undefined
      },
      pids:[],
      // 分类状态数据字典
      statusOptions:[
        {
          "remark": "正常状态",
          "dictLabel": "正常",
          "dictValue": 1,
        },
        {
          "remark": "删除状态",
          "dictLabel": "删除",
          "dictValue": 2,
        }
      ],
      // 类别数据字典
      typeOptions:[
        {
          "remark": "支出类别",
          "dictLabel": "支出",
          "dictValue": 1,
        },
        {
          "remark": "收入类别",
          "dictLabel": "收入",
          "dictValue": 2,
        }
      ],

      // 支出类型的一级目录id
      payPids:[],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名字不能为空", trigger: "blur" }
        ],

      }
    }
  },
  created() {
    this.getList();
    this.getIncomePids()
    this.getPayPids()
  },
  methods: {
    // 重置form表单字段
    reset() {
      this.form = {
        id: undefined,
        name:'',
        pid:'',
        type:1,
        // status:
        remark:'',

      }
      // 对refname=form的整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.resetForm("form");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateBillCategory(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBillCategory(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      // this.getTreeselect();
      if (row.id) {
        // this.form.parentMenuId = row.id;
      } else {
        // this.form.parentMenuId = 0;
      }
      this.getPids('1')
      this.open = true;
      this.title = "添加分类";
    },
    // 获取对应类型的一级目录
      getPids(val) {
        const type = {}
        type.id = val
        searchFirstCategory(type).then(response => {
          this.pids = response.data
        })

      },
    // 获取支出类型的一级目录id
    getPayPids() {

    },
    // 获取收入类型的一级目录id
    getIncomePids() {},
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // this.getTreeselect();
      getMenu(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分类";
      });
    },
    getList() {
      listBillCategory(this.queryParams).then(response => {
       this.billCategoryList = response.rows
       this.loading = false;
      });
    }
  }
}
</script>

